#include "stdafx.h"
#include "CMySpi.h"
#include "CMyApi.h"
#include <string.h>
#include <unistd.h>
#include <iostream>

using namespace std;


CMySpi::CMySpi(CMyApi* pApi)
{
	m_pApi = pApi;
}

CMySpi::~CMySpi()
{
}

//前置session创建反馈
void CMySpi::OnFrontConnected()
{
	gettimeofday(&endtime, NULL);
	timegap = (endtime.tv_sec - m_pApi->starttime.tv_sec) * 1000 + (endtime.tv_usec - m_pApi->starttime.tv_usec) / 1000;
	printf("front connected!!\r\n");
	cout << "cost time: " << timegap << " ms" << endl;

	//设置返回标示，唤醒阻塞线程
	m_pApi->serverrt = true;
	m_pApi->cond.notify_all();

}

//断链反馈函数
void CMySpi::OnFrontDisconnected(int nReason)
{
	printf("front netconnect cutdown!! reason=%d\r\n", nReason);
	//cout << "front netconnect cutdown!! reason=" << nReason  << endl;
	//m_pApi->Destroy();
}

//客户端认证响应函数，通过判断客户端认证是否正常，进行下一步操作，其中有涉及多对多中继模式报送内容
void CMySpi::OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{

	gettimeofday(&endtime, NULL);
	timegap = (endtime.tv_sec - m_pApi->starttime.tv_sec) * 1000 + (endtime.tv_usec - m_pApi->starttime.tv_usec) / 1000;
	//pReject->m_ReasonText为GB2312字符串 ansUTF8为所求UTF8字符串
	GB2312ToUTF8(pRspInfo->ErrorMsg, sizeof(pRspInfo->ErrorMsg), tmpmsg, sizeof(tmpmsg));

	//if判断认证是否成功，成功则进行登录，失败则报错
	if (pRspInfo && pRspInfo->ErrorID == 0)
	{

		printf("auth success\r\n");
		cout << "cost time: " << timegap << " ms" << endl;
		printf("ErrorID=%d  Errormsg=%s\r\n", pRspInfo->ErrorID, tmpmsg);
		
	
		//判断是否是多对多类型报送，如果是，则插入报送条目
		//注意多对多报送是在认证成功后，登录发起前调用
		if (m_pApi->sysinfo_type == 2)
		{
			printf("start sysinfo 2 upload\r\n");
			m_pApi->UserInfoRegister();
		}

		printf("start userlogin\r\n");
		//m_pApi->RequestLogin();
	}
	else
	{
		printf("auth error: ErrorID=%d  Errormsg=%s\r\n", pRspInfo->ErrorID, tmpmsg);
		cout << "cost time: " << timegap << " ms" << endl;
		printf("please press enter\r\n");
		getchar();
		//exit(1);
	}
	//设置返回标示，唤醒阻塞线程
	m_pApi->serverrt = true;
	m_pApi->cond.notify_all();
}

//自定义用户登录反馈函数，其中有涉及一对多中继模式报送内容
void CMySpi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
	gettimeofday(&endtime, NULL);
	timegap = (endtime.tv_sec - m_pApi->starttime.tv_sec) * 1000 + (endtime.tv_usec - m_pApi->starttime.tv_usec) / 1000;
	//pReject->m_ReasonText为GB2312字符串 ansUTF8为所求UTF8字符串
	GB2312ToUTF8(pRspInfo->ErrorMsg, sizeof(pRspInfo->ErrorMsg), tmpmsg, sizeof(tmpmsg));
	if (pRspInfo && pRspInfo->ErrorID == 0)
	{
		printf("%s user login success!\r\n", pRspUserLogin->UserID);
		cout << "cost time: " << timegap << " ms" << endl;
		printf("ErrorID=%d  Errormsg=%s\r\n", pRspInfo->ErrorID, tmpmsg);


		//判断是否是一对多报送，如果是一对多报送，则进行两次测试报送
		//注意一对多报送是在登录完成后调用
		if (m_pApi->sysinfo_type == 3)
		{
			printf("submit count:1\r\n");
			m_pApi->UserInfoSubmit();
			sleep(2);
			printf("submit count:2\r\n");
			m_pApi->UserInfoSubmit();
		}

	}
	else
	{
		printf("ErrorID=%d  Errormsg=%s\r\n", pRspInfo->ErrorID, tmpmsg);
		cout << "cost time: " << timegap << " ms" << endl;
	}
	//设置返回标示，唤醒阻塞线程
	m_pApi->serverrt = true;
	m_pApi->cond.notify_all();
}


//行情快照查询回调函数，用于统计订阅行情的合约列表
void CMySpi::OnRspQryDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
	tmpinstrid = pDepthMarketData->InstrumentID;
	//剔除如下关键字的合约内容
	if(tmpinstrid.find("&") == -1 
	&& tmpinstrid.find("ESP") == -1 
	&& tmpinstrid.find("esp") == -1
	&& tmpinstrid.find("EFP") == -1
	&& tmpinstrid.find("efp") == -1
	)
	{
		//操作API实例中的指针数组，用于存储合约ID，以及对于合约数量进行统计
		m_pApi->instrumentlist[m_pApi->instrcount] = (char*)malloc(81 * sizeof(char));
		strcpy(m_pApi->instrumentlist[m_pApi->instrcount], pDepthMarketData->InstrumentID);
		m_pApi->instrcount++;
	}


	//设置返回标示，唤醒阻塞线程
	if(bIsLast == true)
	{
		m_pApi->serverrt = true;
		m_pApi->cond.notify_all();
	}

	
}